import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { StorageProvider } from '../storage/storage';


/*
  Generated class for the LoginProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class LoginProvider {
  private server_url: string;

  constructor(
    public http: HttpClient,
    private storage: StorageProvider) {
    console.log('Hello LoginProvider Provider');
    //this.server_url = storage.get('server_url');
  }
  login(username: string, password: string) {
    this.server_url = this.storage.get("server_url");
    let formData: FormData = new FormData();
    formData.append('funid', 'login_event');
    formData.append('eventcode', 'cloud_login');
    formData.append('pagetype', 'login');
    formData.append('not_verify', '1');
    formData.append('appid', 'com.wt.wms');
    formData.append('timestamp', this.getTimeStamp());
    formData.append('user_code', username);
    formData.append('user_pwd', password);
    return this.http.post(this.server_url, formData);
  }

  logout() {
    let formData: FormData = new FormData();
    formData.append('funid', 'login');
    formData.append('eventcode', 'logout');
    formData.append('pagetype', 'login');
    return this.http.post(this.server_url, formData);
  }

  getTimeStamp() {
    var now = new Date();
    var timestamp = now.getFullYear() + ''
      + ((now.getMonth() + 1) >= 10 ? "" + (now.getMonth() + 1) : "0"
        + (now.getMonth() + 1))
      + (now.getDate() >= 10 ? now.getDate() : "0"
        + now.getDate())
      + (now.getHours() >= 10 ? now.getHours() : "0"
        + now.getHours())
      + (now.getMinutes() >= 10 ? now.getMinutes() : "0"
        + now.getMinutes())
      + (now.getSeconds() >= 10 ? now.getSeconds() : "0"
        + now.getSeconds());
    return timestamp;
  };
}
